﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace CG67.EspaceRencontre.Outils
{
    public class OutilsGestionRendezVous
    {

        #region BuildDataSet
        /// <summary> 
        /// Méthode qui permet de construire un DataSet à partir d'une requête 
        /// </summary> 
        /// <param name="CommandText">Requête SQL</param> 
        /// <param name="connectionString">Paramètres de chaine connexion</param>
        /// <returns>DataSet</returns> 
        public static DataSet BuildDataSet(String CommandText, ConnectionStringSettings connectionString)
        {
            DataSet ds = null;
            DbProviderFactory factory = DbProviderFactories.GetFactory(connectionString.ProviderName);
            using (IDbConnection connection = factory.CreateConnection())
            {
                //attribution de la chaine de connexion à l'objet connection 
                connection.ConnectionString = connectionString.ConnectionString;
                connection.Open();
                //création et configuration de l'objet command 
                IDbCommand command = factory.CreateCommand();
                command.CommandText = CommandText;
                command.Connection = connection;
                IDbDataAdapter sqlAdapt = factory.CreateDataAdapter();
                sqlAdapt.SelectCommand = command;
                ds = new DataSet("dataset");
                sqlAdapt.Fill(ds);
                connection.Close();
                connection.Dispose();
            }
            return ds;
        }
        #endregion

        #region ExecuteNonQuery
        /// <summary>
        /// Methode executant la requête passée en paramètre
        /// </summary>
        /// <param name="CommandText">Requête SQL</param>
        /// <param name="connectionString">Paramètres de chaine de connexion</param>
        public static void ExecuteNonQuery(string CommandText, ConnectionStringSettings connectionString)
        {
            DbProviderFactory factory = DbProviderFactories.GetFactory(connectionString.ProviderName);
            using (IDbConnection connection = factory.CreateConnection())
            {
                //attribution de la chaine de connexion à l'objet connection 
                connection.ConnectionString = connectionString.ConnectionString;
                connection.Open();
                //création et configuration de l'objet command 
                IDbCommand command = factory.CreateCommand();
                command.CommandText = CommandText;
                command.Connection = connection;
                //éxécution de la requête
                command.ExecuteNonQuery();
                connection.Close();
                connection.Dispose();
            }
        }
        #endregion
    }
}
